package ceri.m2.project.freebase;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import com.freebase.api.Freebase;
import com.freebase.json.JSON;
import static com.freebase.json.JSON.o;
import static com.freebase.json.JSON.a;

public class Article {
	private Util util;
	private String transServiceURL = "http://api.freebase.com/api/trans/raw/guid/";
	
	public Article()
	{
		util = new Util();
	}
	public String getInfos(String name) {
		String result = "";
		Freebase freebase = Freebase.getFreebase();
		JSON results = freebase.search(name);
		System.out.println(results.toString());
		int taille = results.get("result").array().size();
		for (int i = 0; i < taille; i++) {
			String identifiant = results.get("result").get(i).get("id")
					.toString();
			result += "Idetifiant de l'entite:" + identifiant + "\n";
			String score = results.get("result").get(i).get("relevance:score")
					.toString();
			result += "Score de l'entite:" + score + "\n";
		}
		return result;
	}

	public JSON getGuidImage(String nameEntite, String idEntite) {

		Freebase freebase = Freebase.getFreebase();

		JSON query = o("/common/topic/image", a(o("guid", null)), "id",
				idEntite, "name", nameEntite

		);

		JSON reponse = freebase.mqlread(query);
		JSON guid = reponse.get("result").get("/common/topic/image");

		// System.out.println(guid.get(0).get("guid"));

		return guid;
	}

	public JSON getGuidArticle(String nameEntite, String idEntite) {

		Freebase freebase = Freebase.getFreebase();

		JSON query = o("/common/topic/article", a(o("guid", null)), "id",
				idEntite, "name", nameEntite

		);

		JSON reponse = freebase.mqlread(query);
		JSON guid = reponse.get("result").get("/common/topic/article");

		// System.out.println(guid.get(0).get("guid"));

		return guid;
	}

	public JSON getDescription(String nameEntite, String idEntite) throws IOException {
		URL url;
		BufferedReader reader = null;
		HttpURLConnection httpc = null;
		String inputLine = null;
		String result = "";		
		JSON json = JSON.o();
		
		String guid = getGuidArticle(nameEntite, idEntite).get(0).get("guid").toString().replaceAll("#", "").replaceAll("\"", "");
		String descriptionUrl = transServiceURL + guid; 

		url = new URL(descriptionUrl);

		httpc = (HttpURLConnection) url.openConnection();

		reader = new BufferedReader(new InputStreamReader(
				httpc.getInputStream()));

		while ((inputLine = reader.readLine()) != null) {
			result += inputLine + " ";
		}	
		reader.close();
		httpc.disconnect();
		
		result =  util.removeParentheses(util.removeTag(util.getFirstSentence(result), "<p>")) + ".";
		json.put("des", result);
		return json;
	}

	public static void main(String[] args) throws IOException {
		Article article = new Article();
		/* JSON json = article.getGuidArticle("barack obama", "/en/barack_obama");
		 System.out.println(json.get(0).get("guid"));*/
		System.out.println(article.getDescription("barack obama", "/en/barack_obama"));
	}
}
